package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Role;
import com.lei.service.RoleService;
import com.lei.service.impl.RoleServiceImpl;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Lei Chu
 * @Date 2020/10/17 19:47
 */
@WebServlet("/roleServlet/*")
public class RoleServlet extends BaseServlet{

    private final RoleService roleService = new RoleServiceImpl();

    /**
     * 功能描述:返回所有职位的id和name用于添加员工时候的select生成
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllRole(HttpServletRequest request , HttpServletResponse response){
        return JSON.toJSONString(roleService.getAllRole());
    }

    /**
     * 功能描述:分页获得所有的角色，用于表格的生成
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllRoleByPage(HttpServletRequest request,HttpServletResponse response){
        Role role = new Role();
        TransBean.populate(role, request.getParameterMap());
        PageBean page = roleService.getAllRoleByPage(role);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:添加或者修改角色
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String addRole(HttpServletRequest request,HttpServletResponse response){
        Role role = new Role();
        TransBean.populate(role,request.getParameterMap());
        int result = roleService.addRole(role);
        return result+"";
    }

    /**
     * 功能描述:检测添加、修改角色时角色名字是否可用
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String checkAddName(HttpServletRequest request,HttpServletResponse response){
        Role role = new Role();
        TransBean.populate(role,request.getParameterMap());
        String result = roleService.checkAddName(role.getRid(), role.getRname());
        return result;
    }

    /**
     * 功能描述:根据ID删除角色
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String deleteRole(HttpServletRequest request,HttpServletResponse response){
        String rid = request.getParameter("rid");
        int result = roleService.deleteRole(Integer.parseInt(rid));
        return result+"";
    }

}
